import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'dart:ui';

class buttonInfo {
  buttonInfo({
    this.icon,
    this.onTap,
    this.type,
  });
  int icon;
  String type;
  Function onTap;
}

class MyAppBar extends StatefulWidget {
  MyAppBar({
    Key key,
    this.title = '标题',
    this.subtitle,
    this.leading,
    this.actions,
    this.backgroundColor,
    this.textColor,
    this.elevation = 4.0,
    this.noImage = false,
    this.brightness = true,
  }):super(key: key);
  String title; // 标题
  String subtitle; // 标题
  buttonInfo leading; // 右侧按钮
  buttonInfo actions; // 左侧按钮
  Color backgroundColor; // 背景颜色
  Color textColor; // 文字内容颜色
  double elevation; // AppBar阴影
  bool noImage; // 是否有背景图片
  bool brightness; // 状态栏图标是否是白色
  _MyAppBarState createState() => _MyAppBarState();
}

class _MyAppBarState extends State<MyAppBar> {
  @override
  Widget leading (double contHeight, bool canPop) {
    if (widget.leading == null) {
      if (canPop) {
        return Container(
          height: contHeight,
          width: contHeight,
          child: Center(
            child: IconButton(
              icon: Icon(
                IconData(0xe622, fontFamily: 'iconfont'),
                size: ScreenUtil().setWidth(36),
                color: widget.textColor == null ? Theme.of(context).primaryIconTheme.color :widget.textColor,
              ),
              onPressed: (){
                Navigator.pop(context);
              },
            ),
          ),
        );
      }
      return Container(
        height: contHeight,
        width: contHeight,
      );
    }
    return Container(
      height: contHeight,
      width: contHeight,
      child: Center(
        child: IconButton(
          icon: Icon(
            IconData(widget.leading.icon, fontFamily: 'iconfont'),
            size: ScreenUtil().setWidth(36),
            color: widget.textColor == null ? Theme.of(context).primaryIconTheme.color :widget.textColor,
          ),
          onPressed: widget.leading.type == 'openDrawer' ? () {
            Scaffold.of(context).openDrawer();
          } : widget.leading.onTap,
        ),
      ),
    );
  }

  Widget actions (contHeight) {
    if (widget.actions == null) {
      return Container(
        height: contHeight,
        width: contHeight,
      );
    }
    return Container(
      height: contHeight,
      width: contHeight,
      child: Center(
        child: IconButton(
          icon: Icon(
            IconData(widget.actions.icon, fontFamily: 'iconfont'),
            size: ScreenUtil().setWidth(36),
            color: widget.textColor == null ? Theme.of(context).primaryIconTheme.color :widget.textColor,
          ),
          onPressed: widget.actions.onTap,
        ),
      ),
    );
  }

  Widget build(BuildContext context) {
    final ModalRoute<dynamic> parentRoute = ModalRoute.of(context);
    final bool canPop = parentRoute?.canPop ?? false;
    double contHeight = ScreenUtil().setWidth(124) - MediaQueryData.fromWindow(window).padding.top;
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: widget.brightness ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
      child: Container(
        padding: EdgeInsets.only(top: MediaQueryData.fromWindow(window).padding.top),
        decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Colors.black,
              offset: Offset(0, 0),
              blurRadius: widget.elevation,
              spreadRadius: 0.0
            )
          ],
          color: widget.backgroundColor == null ? Theme.of(context).primaryColor : widget.backgroundColor,
          image: widget.noImage == false ? DecorationImage(
            image: AssetImage('static/appbarBg.png'),
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(
              widget.backgroundColor == null ? Theme.of(context).primaryColor : widget.backgroundColor, BlendMode.color
            )
          ) : null,
        ),
        height: ScreenUtil().setWidth(124),
        child: Container(
          child: Row(
            children: <Widget>[
              leading(contHeight,canPop),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      widget.title,
                      style: TextStyle(
                        fontSize: widget.subtitle != null ? ScreenUtil().setWidth(28) : ScreenUtil().setWidth(36),
                        color: widget.textColor == null ? Theme.of(context).primaryIconTheme.color :widget.textColor,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    widget.subtitle != null ?
                    Text(
                      widget.subtitle,
                      style: TextStyle(
                        color: widget.textColor == null ? Theme.of(context).primaryIconTheme.color :widget.textColor,
                        fontSize: ScreenUtil().setWidth(18),
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ):SizedBox(height: 0,)
                  ],
                ),
              ),
              actions(contHeight)
            ],
          ),
        ),
      )
    );
  }
}